package com.walter.learn.Util;

import lombok.extern.slf4j.Slf4j;

import java.applet.Applet;
import java.io.*;
import java.util.List;
import java.util.Scanner;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * @ProjectName: learnDemo
 * @Package: com.walter.util
 * @ClassName: FileUtils
 * @Author: DELL
 * @Description: 关于文件操作的工具类
 * @Date: 2019/8/7 20:01
 * @Version: 1.0
 */
@Slf4j
public class FileUtils {

    private static String resultType_Name = "name";
    private static String resultType_NameAbsolutePath = "absolutePath";

    /**
     * @Description 将文件打成zip包
     * @Param sourceFilePath 指定文件路径
     * @Param zipFilePath 打成压缩包的路径
     * @Param fileName文件名称
     * @Create 2019/8/14 19:26
     * @Author wangzheng
     */
    public static boolean fileToZip(String sourceFilePath, String zipFilePath, String fileName) {
        Scanner scanner = new Scanner(System.in);
        Applet applet = new Applet();
        boolean flag = false;
        File sourceFile = new File(sourceFilePath);
        FileInputStream fis = null;
        BufferedInputStream bis = null;
        FileOutputStream fos = null;
        ZipOutputStream zos = null;

        if (sourceFile.exists() == false) {
            System.out.println("待压缩的文件目录：" + sourceFilePath + "不存在.");
        } else {
            try {
                File zipFile = new File(zipFilePath + "/" + fileName + ".zip");
                if (zipFile.exists()) {
                    System.out.println(zipFilePath + "目录下存在名字为:" + fileName + ".zip" + "打包文件.");
                    System.out.println("确认继续打包吗?Y/N");
                    String confirmMessage = scanner.next();
                } else {
                    File[] sourceFiles = sourceFile.listFiles();
                    if (null == sourceFiles || sourceFiles.length < 1) {
                        System.out.println("待压缩的文件目录：" + sourceFilePath + "里面不存在文件，无需压缩.");
                    } else {
                        fos = new FileOutputStream(zipFile);
                        zos = new ZipOutputStream(new BufferedOutputStream(fos));
                        byte[] bufs = new byte[1024 * 10];
                        for (int i = 0; i < sourceFiles.length; i++) {
                            //创建ZIP实体，并添加进压缩包
                            ZipEntry zipEntry = new ZipEntry(sourceFiles[i].getName());
                            zos.putNextEntry(zipEntry);
                            //读取待压缩的文件并写进压缩包里
                            fis = new FileInputStream(sourceFiles[i]);
                            bis = new BufferedInputStream(fis, 1024 * 10);
                            int read = 0;
                            while ((read = bis.read(bufs, 0, 1024 * 10)) != -1) {
                                zos.write(bufs, 0, read);
                            }
                        }
                        flag = true;
                    }
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            } catch (IOException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            } finally {
                //关闭流
                try {
                    if (null != bis) bis.close();
                    if (null != zos) zos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    throw new RuntimeException(e);
                }
            }
        }
        return flag;
    }



    /**
     * @Description 递归遍历文件目录
     * @Param path 文件路径
     * @Param suffix 要获取的文件后缀(如果为*,则表示查询所有类型)
     * @Param resultType 获取的结果类型,文件名为Name,文件绝对路径为AbsolutePath
     * @Create 2019/8/14 19:48
     * @Author wangzheng
     */
    public static void getFilesList(String path, String suffix, String resultType, List<String> fileList) {
        File file = new File(path);
        if (file.exists()) {
            File[] files = file.listFiles();
            if (files == null || files.length == 0) {
                System.out.println(file.getName());
            } else {
                for (File temp : files) {
                    if (temp.isDirectory()) {
                        getFilesList(temp.getAbsolutePath(), suffix, resultType, fileList);
                    } else if (temp.isFile()) {
                        //这里输出的是mp4文件，如果要输出所有文件，去掉这个判断即可
                        if (resultType.equals(resultType_Name)) {//获取文件名
                            if (temp.getName().endsWith(suffix)) {//只添加后缀为suffex的文件名
                                fileList.add(temp.getName());
                                System.out.println(temp.getName());
                            }
                            fileList.add(temp.getName());
                        } else if (resultType.equals(resultType_NameAbsolutePath)) {//获取文件的绝对路径
                            if (temp.getName().endsWith(suffix)) {//只添加后缀为suffex的文件名
                                fileList.add(temp.getAbsolutePath());
                                System.out.println(temp.getName());
                            }
                            fileList.add(temp.getAbsolutePath());
                        } else {
                            fileList.add(temp.getAbsolutePath());
                        }
                    }
                }
            }
        } else {
            System.out.println("文件或目录不存在!");
        }
    }


}
